package com.shop.controller;

import com.github.pagehelper.PageInfo;
import com.shop.Service.ClassifyService;
import com.shop.Service.LogService;
import com.shop.Utils.TokenUtils;
import com.shop.common.TokenService;
import com.shop.pojo.Classifies;
import com.shop.pojo.Classify;
import com.shop.pojo.Good;
import com.shop.pojo.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@Controller
@RequestMapping("/api/classify")
public class ClassifyController {
    @Autowired
    private ClassifyService classifyService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private LogService logService;
    @PostMapping("/addClassify")
    @ResponseBody
    public Message<String> addUser(@RequestHeader(value = "Authorization",required = true) String Authorization,@RequestBody Map<String,Object> map) throws Exception{
        Message<String> message = new Message<String>();
        if (TokenUtils.verify(Authorization) !=true){
            message.setStatus("401");
            message.setMessage("无权限");
            return message;
        }
        Classify classify = new Classify();
        String fname = map.get("fname").toString();
        classify.setFname(fname);
        if (map.get("fatherId")!=null){
            int fatherId = Integer.parseInt(map.get("fatherId").toString());
            if (fatherId>=0){
                classify.setFatherId(fatherId);
            }
        }

        message = classifyService.addClassify(classify);
        if (message.getData()==null){
            logService.addLog(tokenService.getUsername(Authorization),false,fname+"分类的添加");
        }else {
            logService.addLog(tokenService.getUsername(Authorization),true,fname+"分类的添加");
        }
        message.setStatus("201");
        return message;
    }
    @PostMapping("/selectAllClassify")
    @ResponseBody
    public Message<PageInfo<Classify>> selectAllClassify(@RequestHeader(value = "Authorization",required = true) String Authorization,@RequestBody Map<String,Object> map) throws Exception{
        Message<PageInfo<Classify>> message = new Message<>();
        if (TokenUtils.verify(Authorization) !=true){
            message.setStatus("401");
            message.setMessage("无权限");
            return message;
        }
        String queryInfo = ""+map.get("queryInfo");
        int pageSize =  Integer.parseInt(map.get("pageSize").toString());
        int pageNum = Integer.parseInt(map.get("pageNum").toString());
        message = classifyService.selectAllClassify(queryInfo,pageSize,pageNum);
        message.setStatus("200");
        return message;
    }
    @PostMapping("/deleteClassify")
    @ResponseBody
    public Message<String> deleteClassify(@RequestHeader(value = "Authorization",required = true) String Authorization,@RequestBody Map<String,Object> map) throws Exception{
        Message<String> message = new Message<>();
        if (TokenUtils.verify(Authorization) !=true){
            message.setStatus("401");
            message.setMessage("无权限");
            return message;
        }
        int id =  Integer.parseInt(map.get("id").toString());
        message = classifyService.deleteClassify(id);
        if (message.getData()==null){
            logService.addLog(tokenService.getUsername(Authorization),false,id+"分类的删除");
        }else {
            logService.addLog(tokenService.getUsername(Authorization),true,id+"分类的删除");
        }
        message.setStatus("201");
        return message;
    }
    @PostMapping("/updateClassify")
    @ResponseBody
    public Message<String> updateClassify(@RequestHeader(value = "Authorization",required = true) String Authorization,@RequestBody Map<String,Object> map) throws Exception{
        Message<String> message = new Message<>();
        if (TokenUtils.verify(Authorization) !=true){
            message.setStatus("401");
            message.setMessage("无权限");
            return message;
        }
        Classify classify = new Classify();
        int fid =  Integer.parseInt(map.get("fid").toString());
        classify.setFid(fid);
        String fname = map.get("fname").toString();
        classify.setFname(fname);
        if (map.get("fatherId")!=null){
            int fatherId = Integer.parseInt(map.get("fatherId").toString());
            classify.setFatherId(fatherId);
        }
        message = classifyService.updateClassify(classify);
        if (message.getData()==null){
            logService.addLog(tokenService.getUsername(Authorization),false,fname+"分类的更新");
        }else {
            logService.addLog(tokenService.getUsername(Authorization),true,fname+"分类的更新");
        }
        message.setStatus("201");
        return message;
    }
    @RequestMapping("/selectAll")
    @ResponseBody
    public Message<List<Classify>> selectAll(@RequestHeader(value = "Authorization",required = true) String Authorization) throws Exception{
        Message<List<Classify>> message = new Message<>();
        if (TokenUtils.verify(Authorization) !=true){
            message.setStatus("401");
            message.setMessage("无权限");
            return message;
        }
        message  = classifyService.selectAll();
        message.setStatus("200");
        return message;
    }
    @PostMapping("/selectNew")
    @ResponseBody
    public Message<PageInfo<Classifies>> selectNew(@RequestHeader(value = "Authorization",required = true) String Authorization,@RequestBody Map<String,Object> map) throws Exception{
        Message<PageInfo<Classifies>> message = new Message<>();
        if (TokenUtils.verify(Authorization) !=true){
            message.setStatus("401");
            message.setMessage("无权限");
            return message;
        }
        String queryInfo = ""+map.get("queryInfo");
        int pageSize =  Integer.parseInt(map.get("pageSize").toString());
        int pageNum = Integer.parseInt(map.get("pageNum").toString());
        message = classifyService.selectNew(queryInfo,pageSize,pageNum);
        message.setStatus("200");
        return message;
    }
    @PostMapping("/selectById")
    @ResponseBody
    public Message<Classify> selectById(@RequestBody Map<String,Object> map) throws Exception {
        Message<Classify> message =new Message<>();
        int pid =  Integer.parseInt(map.get("fid").toString());
        message = classifyService.selectById(pid);
        message.setStatus("200");
        return message;
    }


}
